package com.mollytask.institution;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.mollytask.R;
import com.mollytask.base.BaseDetailActivity;
import com.mollytask.bean.CheckRecord;
import com.mollytask.bean.DataFill;
import com.mollytask.bean.Institution;
import com.mollytask.bean.Message;
import com.mollytask.util.RegexUtil;

import java.util.List;

import butterknife.BindView;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class ReviewResultActivity extends BaseDetailActivity {


    @BindView(R.id.check_result_status_text)
    TextView mCheckStatus;
    @BindView(R.id.check_result_time)
    TextView mCheckTime;
    @BindView(R.id.check__result_name)
    TextView mCheckName;
    @BindView(R.id.check_result_message_text)
    TextView mCheckMessage;

    @BindView(R.id.re_fill)
    Button mReFillBtn;


    @Override
    public int setLayout() {
        return R.layout.activity_review_result;
    }

    @Override
    public void receiveData() {
        mDataFill = (DataFill) getIntent().getSerializableExtra("dataFillResult");
        mInstitution = (Institution) getIntent().getSerializableExtra("institution");
    }

    @Override
    public void init() {


        mCheckName.setText(mDataFill.getmInName());
        mCheckTime.setText(mDataFill.getmFillId().substring(9, 13) + "年第" + mDataFill.getmFillId().substring(13, 14) + "季度");

        if (mDataFill.getStatus().equals(0)) {
            mCheckStatus.setTextColor(getResources().getColor(android.R.color.holo_red_light));
            mCheckStatus.setText("待审核");

        } else if (mDataFill.getStatus().equals(1)) {
            mCheckStatus.setTextColor(getResources().getColor(R.color.status_yes));

            mCheckStatus.setText("已通过");


        } else if (mDataFill.getStatus().equals(2)) {
            mCheckStatus.setTextColor(getResources().getColor(R.color.status_not));
            mReFillBtn.setVisibility(View.VISIBLE);
            mCheckStatus.setText("未通过");

        }


        BmobQuery<CheckRecord> bmobQuery = new BmobQuery<>();
        bmobQuery.addWhereEqualTo("fillId", mDataFill.getmFillId());
        bmobQuery.findObjects(new FindListener<CheckRecord>() {
            @Override
            public void done(List<CheckRecord> list, BmobException e) {
                if (e == null) {
                    mCheckMessage.setText(list.get(0).getCheckMessage());
                }
            }
        });


        mReFillBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setCanEdit();
                mReSubmit.setVisibility(View.VISIBLE);
            }
        });

        mReSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reFill();
            }
        });
    }

    private void setCanEdit() {
        et_code.setEnabled(true);
        et_name.setEnabled(true);
        et_type.setEnabled(true);
        et_adress.setEnabled(true);
        et_post_code.setEnabled(true);
        et_email.setEnabled(true);
        et_phone.setEnabled(true);
        et_fuzeren.setEnabled(true);
        et_usenum.setEnabled(true);
        et_hotarea.setEnabled(true);
        et_coldarea.setEnabled(true);
        et_stati_name.setEnabled(true);
        et_stati_phone.setEnabled(true);
        et_waterPrice.setEnabled(true);
        et_coalAmount.setEnabled(true);
        et_coalPrice.setEnabled(true);
        et_elecAmount.setEnabled(true);
        et_elecPrice.setEnabled(true);
        et_gasAmount.setEnabled(true);
        et_gasoglineAmount.setEnabled(true);
        et_gasolinePrice.setEnabled(true);
        et_gasPrice.setEnabled(true);
        et_waterAmount.setEnabled(true);

    }

    public void reFill() {
        boolean cancel = false;

        if (TextUtils.isEmpty(et_usenum.getText().toString())
                || TextUtils.isEmpty(et_hotarea.getText().toString())
                || TextUtils.isEmpty(et_coldarea.getText().toString())
                || TextUtils.isEmpty(et_stati_name.getText().toString())
                || TextUtils.isEmpty(et_stati_phone.getText().toString())
                || TextUtils.isEmpty(et_waterAmount.getText().toString())
                || TextUtils.isEmpty(et_waterPrice.getText().toString())
                || TextUtils.isEmpty(et_coalAmount.getText().toString())
                || TextUtils.isEmpty(et_coalPrice.getText().toString())
                || TextUtils.isEmpty(et_elecAmount.getText().toString())
                || TextUtils.isEmpty(et_elecPrice.getText().toString())
                || TextUtils.isEmpty(et_gasAmount.getText().toString())
                || TextUtils.isEmpty(et_gasPrice.getText().toString())
                || TextUtils.isEmpty(et_gasoglineAmount.getText().toString())
                || TextUtils.isEmpty(et_gasolinePrice.getText().toString())) {
            cancel = true;
            Toast.makeText(ReviewResultActivity.this, "请输入完整信息", Toast.LENGTH_LONG).show();
        } else if (!RegexUtil.checkMobile(et_stati_phone.getText().toString())) {
            Toast.makeText(ReviewResultActivity.this, "手机格式不正确", Toast.LENGTH_LONG).show();
            cancel = true;
        }
        //格式正确
        if (!cancel) {
            DataFill dataFill = new DataFill();

            dataFill.setmFillId(mDataFill.getmFillId());
            dataFill.setmInId(mInstitution.getmInCode());
            dataFill.setmInName(mInstitution.getmInName());
            dataFill.setmSuperCode(mInstitution.getmInSuperAccount());
            dataFill.setmConsumerNo(et_usenum.getText().toString());
            dataFill.setmHeatArea(et_hotarea.getText().toString());
            dataFill.setmCoolArea(et_coldarea.getText().toString());
            dataFill.setElecPrice(et_elecPrice.getText().toString());
            dataFill.setElecAmount(et_elecAmount.getText().toString());
            dataFill.setWaterPrice(et_waterPrice.getText().toString());
            dataFill.setWaterAmount(et_waterAmount.getText().toString());
            dataFill.setCoalPrice(et_coalPrice.getText().toString());
            dataFill.setCoalAmount(et_coalAmount.getText().toString());
            dataFill.setGasPrice(et_gasPrice.getText().toString());
            dataFill.setGasAmount(et_gasAmount.getText().toString());
            dataFill.setGasolinePrice(et_gasolinePrice.getText().toString());
            dataFill.setGasoglineAmount(et_gasoglineAmount.getText().toString());
            dataFill.setmStatisticianName(et_stati_name.getText().toString());
            dataFill.setmStatisticianTel(et_stati_phone.getText().toString());
            dataFill.setStatus(0);
            dataFill.update(mDataFill.getObjectId(), new UpdateListener() {
                @Override
                public void done(BmobException e) {
                    if (e == null) {
                        Toast.makeText(ReviewResultActivity.this, "提交成功", Toast.LENGTH_LONG).show();

                        Message message = new Message();
                        message.setmFromCode(mInstitution.getmInCode());
                        message.setmToCode(mInstitution.getmInSuperAccount());
                        message.setType(1);
                        message.setmContent(mInstitution.getmInName() + "重新提交了" + dataFill.getmFillId().substring(9, 13) + "年第" + dataFill.getmFillId().substring(13, 14) + "季度的数据，请注意查看");
                        message.save(new SaveListener<String>() {
                            @Override
                            public void done(String s, BmobException e) {

                            }
                        });

                    } else {
                        Toast.makeText(ReviewResultActivity.this, "提交失败" + e.getMessage().toString(), Toast.LENGTH_LONG).show();
                    }
                }
            });

            finish();

        }
    }


}
